package com.bysj.yqsht.service.impl;

import com.bysj.yqsht.bean.Classes;
import com.bysj.yqsht.bean.MatchsApplyExample;
import com.bysj.yqsht.bean.Student;
import com.bysj.yqsht.bean.StudentExample;
import com.bysj.yqsht.bean.extend.CheckStudentExtend;
import com.bysj.yqsht.bean.extend.MatchsApplyExtend;
import com.bysj.yqsht.bean.extend.StudentExtend;
import com.bysj.yqsht.dao.ClassesMapper;
import com.bysj.yqsht.dao.StudentMapper;
import com.bysj.yqsht.dao.extend.CheckStudentExtendMapper;
import com.bysj.yqsht.dao.extend.MatchsApplyExtendMapper;
import com.bysj.yqsht.dao.extend.StudentExtendMapper;
import com.bysj.yqsht.service.IStudentService;
import com.bysj.yqsht.utils.CustomerException;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class IStudentServiceImpl implements IStudentService {
    @Resource
    private StudentMapper studentMapper;

    @Resource
    private StudentExtendMapper studentExtendMapper;
    @Resource
    private CheckStudentExtendMapper checkStudentExtendMapper;
    @Resource
    private MatchsApplyExtendMapper matchsApplyExtendMapper;
    @Resource
    private ClassesMapper classesMapper;



    @Override
    public List<Student> findAll() {
        StudentExample example = new StudentExample();
        return studentMapper.selectByExample(example);
    }

    @Override
    public void saveOrUpdate(Student student) {
        if(student.getId() != null ){
            studentMapper.updateByPrimaryKey(student);
        }else {
            Classes c1 = classesMapper.selectByPrimaryKey(student.getClassId());
            Integer num = Integer.parseInt(c1.getStudentes());
            num++;
            c1.setStudentes(num.toString());
            classesMapper.updateByPrimaryKey(c1);
            studentMapper.insert(student);
        }
    }

//    @Override
//    public void update(Student student) {
//        studentMapper.updateByPrimaryKey(student);
//    }

    @Override
    public void deleteById(Long id)  {
        checkStudentExtendMapper.deleteByStudentId(id);
        matchsApplyExtendMapper.deleteByMatchsApplyId(id);

        Student s1 = studentMapper.selectByPrimaryKey(id);
        Classes c1 = classesMapper.selectByPrimaryKey(s1.getClassId());
        Integer num = Integer.parseInt(c1.getStudentes());
        num--;
        c1.setStudentes(num.toString());
        classesMapper.updateByPrimaryKey(c1);
        studentMapper.deleteByPrimaryKey(id);

    }

    @Override
    public List<StudentExtend> findAllWithClass(int pageNum,int limit) {
        PageHelper.startPage(pageNum,limit);
        StudentExample studentExample = new StudentExample();
        List<StudentExtend> list1 = studentExtendMapper.selectAllWithClass();
        PageInfo<StudentExtend> data = new PageInfo<StudentExtend>(list1);
        List<StudentExtend> list = data.getList();
        return list;
    }

    @Override
    public List<StudentExtend> findWithAllByName(String name, String number,Long classId) {
        return studentExtendMapper.findWithAllByName(name, number,classId);
    }

    @Override
    public int selectDataCount(Student student) {
        return studentExtendMapper.selectDataCount(student);
    }


}
